package io.renren.controller;

import io.renren.annotation.Login;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.RequestPage;
import io.renren.common.validator.Assert;
import io.renren.entity.JxLaboratoryEntity;
import io.renren.entity.JxLaboratoryManagerEntity;
import io.renren.form.DescForm;
import io.renren.form.ManagerForm;
import io.renren.service.JxLaboratoryManagerService;
import io.renren.service.JxLaboratoryService;
import io.renren.shiro.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  关于实验室的api
 */
@RestController
@RequestMapping("/api/jxLaboratory")
@Api(tags="实验室")
public class ApiJxLaboratoryController {
    @Autowired
    private JxLaboratoryService jxLaboratoryService;
    @Autowired
    private JxLaboratoryManagerService  jxLaboratoryManagerService;

    /**
     * 列表
     */


    @PostMapping("list")
    @ApiOperation("实验室列表")
    public R list(@RequestBody RequestPage  page){
        Assert.isNull(page, "分页数和每页显示不能为空!");
        Map<String,Object> params=new HashMap<String,Object>();
        params.put("limit",page.getLimit());
        params.put("page",page.getPage());
        PageUtils pu = jxLaboratoryService.getAllWithPage(params);
        return R.ok().put("page",pu);
    }


    @PostMapping("desc")
    @ApiOperation("实验室详情")
    public R desc(@RequestBody DescForm form){
        JxLaboratoryEntity entity1=jxLaboratoryService.getById(form.getId());
        return R.ok().put("data",entity1);
    }

    @PostMapping("validatorManager")
    @ApiOperation("实验室管理员验证")
    public R ValidatorManager(@RequestBody ManagerForm managerForm){
      Map<String,Object> params=new HashMap<String,Object>();
      params.put("num",managerForm.getNum());
        List<JxLaboratoryEntity> laboratorys=jxLaboratoryService.getList(params);
        if(laboratorys.size()>0){
            JxLaboratoryEntity laboratory=laboratorys.get(0);
            Long id=laboratory.getId();
        JxLaboratoryManagerEntity  entity=jxLaboratoryManagerService.getlaboratoryId(id);
          if(entity.getName().equals(managerForm.getName())){

            if (!entity.getPassword().equals(ShiroUtils.sha256(managerForm.getPassword(), entity.getSalt()))) {
                throw new RRException("手机号或密码错误");
            }else{
                return  R.ok().put("msg","验证通过！");
            }
          }return R.error("账户名错误");
        }
        return R.error("数据不存在");
    }

}
